package com.jianggujin.demo.admin.handler;

import java.util.Map;

import com.jianggujin.modulelink.config.JModuleConfig;
import com.jianggujin.modulelink.magager.JModuleManager;
import com.jianggujin.modulelink.module.JModule;
import com.jianggujin.modulelink.util.JStringUtils;

import fi.iki.elonen.NanoHTTPD.IHTTPSession;

public class InvokeHandler extends AbstractHandler {

    @Override
    protected Object handle(JModuleManager manager, IHTTPSession session, Map<String, String> params) throws Exception {
        String moduleName = params.get("moduleName");
        if (JStringUtils.isEmpty(moduleName)) {
            return error("moduleName不能为空");
        }
        JModule<JModuleConfig> module = manager.getModule(moduleName);
        String actionName = params.get("actionName");
        Object result = null;
        if (JStringUtils.isEmpty(actionName)) {
            result = module.invokeDefaultAction(params);
        } else {
            result = module.invokeAction(actionName, params);
        }
        return success(result);
    }

}
